<?php

namespace app\common\api\controller;

use think\Controller;

class Relation extends Controller
{
    public function billBelong($billnumber)
    {
        if ($billnumber != '公司') {
            $map['usernumber'] = $billnumber;
            $map['isbill'] = array('egt', 1);
            $bill_info = Db('member')->where($map)->find();
            if (empty($bill_info)) {
                $data['code'] = -3000;
            } else {
                $billid = $bill_info['uid'];
                $data['billcenterid'] = $billid;
                $data['code'] = 1;
            }
        } else {
            $data['billcenterid'] = 0;
            $data['code'] = 1;
        }
        return $data;
    }

    /**
     推荐关系
     */
    public function tRelation($tnumber)
    {
        $tuijian_info = Db('member')->where(array('usernumber' => $tnumber, 'status' => ['>=', 1]))->find();
        if (empty($tuijian_info)) {
            $data['code'] = -1000;
            return $data;//推荐人不存在
        }
        $tuijianid = $tuijian_info['uid'];

        if (empty($tuijian_info['tuijianids'])) {
            $tuijianids = ',' . $tuijianid . ',';//推荐人id组
        } else {
            $tuijianids = $tuijian_info['tuijianids'] . $tuijianid . ',';//推荐人id组
        }

        $tdeep = $tuijian_info['tdeep'] + 1;
        $data['tuijianid'] = $tuijianid;
        $data['tuijiannumber'] = $tnumber;
        $data['tuijianids'] = $tuijianids;
        $data['tdeep'] = $tdeep;
        $data['code'] = 1;
        return $data;
    }

    /**
     * 双规节点关系
     * @param $uid
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function pTwoRelation($uid)
    {
        $parent_info = Db('member')->where(array('uid' => $uid))->find();

        if ($parent_info['left_zone'] == 0) {
            $zone = 1;
        } else {
            if ($parent_info['right_zone'] == 0) {
                $zone = 2;
            } else {
                $this->error('无接点位！');
            }
        }

        $parentid = $parent_info['uid'];
        if (empty($parent_info['parentids'])) {
            $parents = ',' . $parentid . ',';//接点人id组
        } else {
            $parents = $parent_info['parentids'] . $parentid . ',';//接点人id组
        }

        if (!$parent_info['zid']) {
            $zid = 1;//接点人id组
        } else {
            $zid = $parent_info['zid'] . $zone;
        }

        $pdeep = $parent_info['pdeep'] + 1;

        $data['parentid'] = $parentid;
        $data['parentnumber'] = $parent_info['usernumber'];
        $data['parentids'] = $parents;
        $data['zid'] = $zid;
//        $data['parentareas'] = $parentareas;
        $data['pdeep'] = $pdeep;
        $data['zone'] = $zone;
        return $data;
    }


    public function pThreeRelation($uid)
    {
        $parent_info = Db('member')->where(array('uid' => $uid, 'status' => 1))->find();

        if ($parent_info['1_zone'] == 0) {
            $zone = 1;
        } elseif ($parent_info['2_zone'] == 0) {
            $zone = 2;
        } elseif ($parent_info['3_zone'] == 0) {
            $zone = 3;
        } else {
            $this->error('暂无接点位！');
        }

        $parentid = $parent_info['uid'];
        if (empty($parent_info['parentids'])) {
            $parents = ',' . $parentid . ',';//接点人id组
        } else {
            $parents = $parent_info['parentids'] . $parentid . ',';//接点人id组
        }

//        if (empty($parent_info['parentareas'])) {
//            $parentareas = ',' . $parentid . ':' . $zone . ',';//接点人id组
//        } else {
//            $parentareas = $parent_info['parentareas'] . $parentid . ':' . $zone . ',';//接点人id组
//        }

        $pdeep = $parent_info['pdeep'] + 1;
        $zid = $parent_info['zid'] . $zone;

        $data['parentid'] = $parentid;
        $data['parentnumber'] = $parent_info['usernumber'];
        $data['parentids'] = $parents;
//        $data['parentareas'] = $parentareas;
        $data['pdeep'] = $pdeep;
        $data['zone'] = $zone;
        $data['zid'] = $zid;
        return $data;
    }
}